package com.system.presideover.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.system.common.config.exception.CustomException;
import com.system.common.result.Result;
import com.system.presideover.entity.AccountNumber;
import com.system.presideover.service.AccountNumberService;
import com.system.presideover.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 账号管理 前端控制器
 * </p>
 *
 * @author system
 * @since 2024-04-16
 */
@Api(tags = "账号管理")
@RestController
@RequestMapping("/presideover/account-number")
public class AccountNumberController {

@Autowired
    private AccountNumberService accountNumberService;

    @ApiOperation(value = "查看所有账号")
    @GetMapping("/listAccountNumber")
    public Result listAccountNumber(){
        List<AccountNumber> list = accountNumberService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "添加账号")
    @PostMapping("/saveAccountNumber")
    public Result saveAccountNumber(@RequestBody AccountNumber accountNumber){
        accountNumber.setCreateTime(LocalDateTime.now());
        boolean save = accountNumberService.save(accountNumber);

        if (save){
            return Result.ok().message("添加成功");
        }
        return Result.fail().message("添加失败");
    }


    @ApiOperation(value = "修改账号")
    @PostMapping("/updateAccountNumber")
    public Result updateAccountNumber(@RequestBody AccountNumber accountNumber){
        accountNumber.setUpdateTime(LocalDateTime.now());
        boolean save = accountNumberService.updateById(accountNumber);

        if (save){
            return Result.ok().message("修改成功");
        }
        return Result.fail().message("修改失败");
    }

    @ApiOperation(value = "删除账号")
    @PostMapping("/delectAccountNumber")
    public Result delectAccountNumber(@RequestBody AccountNumber accountNumber){
        boolean save = accountNumberService.removeById(accountNumber.getId());

        if (save){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }


    @ApiOperation(value = "登录用户")
    @PostMapping("/login")
    public Result login(@RequestBody AccountNumber loginVo) {
        String username = loginVo.getAccount();
        //2.去数据库查询
        LambdaQueryWrapper<AccountNumber> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AccountNumber::getAccount, username);
        AccountNumber one = accountNumberService.getOne(wrapper);
        if (null == one) {
            throw new CustomException(201, "用户不存在");
        }
//        if(!MD5.encrypt(loginVo.getPassword()).equals(sysUser.getPassword())) {
//        throw new CustomException(201,"密码错误");
//    }
        if (!loginVo.getPassword().equals(one.getPassword())) {
            throw new CustomException(201, "密码错误");
        }

        Map<String, Object> cliams = new HashMap<>();
        cliams.put("id",one.getId());
        String token = JwtUtils.generateJwt(cliams);

        return Result.ok(token).message("登陆成功");
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result logout() {
        return Result.ok();
    }


}

